package com.weiyuwqj.admin.controller.admin.asset;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.weiyuwqj.admin.controller.base.BaseController;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.SysAssetDeposit;
import com.weiyuwqj.api.model.vo.SysAssetDepositVo;
import com.weiyuwqj.api.service.SysAssetDepositService;
import com.weiyuwqj.api.util.IpUtils;

/**
 * 
 * SysAssetDepositController	
 * @Description: 资产方信用controller
 * @author cx
 * @date:2017年3月25日上午11:27:40 
 *
 */
@RestController
@RequestMapping(value = "/admin/assetDeposit")
public class SysAssetDepositController extends BaseController{
	
	@Autowired
	private SysAssetDepositService sysAssetDepositService;
	
	/**
	 * 列表分页
	 * @param name
	 * @return
	 */
	@RequestMapping(value ="/getPageList",method = RequestMethod.POST)
	public PageList getPageList(final Paginator p,final Integer companyId){
		final HashMap<String, Object> map = new HashMap<String, Object>();
		if(companyId!=null&&companyId>0){
			map.put("companyId", companyId);
		}
		return this.sysAssetDepositService.getPageList(p,map);
	}
	
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value ="/findById",method = RequestMethod.POST)
	public SysAssetDeposit findById(final Integer id){
		return this.sysAssetDepositService.findById(id);
	}
	
	/**
	 * 保存
	 * @param assetCompany
	 * 页面上需将此条记录的所有属性传入后台
	 * @return
	 */
	@RequestMapping(value ="/save",method = RequestMethod.POST)
	public HashMap<String,String> save(final SysAssetDepositVo sysAssetDeposit){
		sysAssetDeposit.validData();
		sysAssetDeposit.setCreator(getSessionUserId());
		sysAssetDeposit.setAddip(IpUtils.get());
		sysAssetDeposit.setAddtime(new Date());
		sysAssetDeposit.setTotalAmount(new BigDecimal(sysAssetDeposit.getDeposit()).
						divide(new BigDecimal(sysAssetDeposit.getRate()), RoundingMode.HALF_UP).doubleValue());
		if(sysAssetDeposit.getId()!=null && sysAssetDeposit.getId()>0){
			this.sysAssetDepositService.updateNotNull(sysAssetDeposit);
		}else{
			final SysAssetDeposit existAssetDeposit = this.sysAssetDepositService.findByCompanyId(sysAssetDeposit.getCompanyId());
			if(existAssetDeposit!=null){
				throw new CrowdException("sys.asset.deposit.exist.company");
			}
			this.sysAssetDepositService.save(sysAssetDeposit);
		}
		return Message.OK;
	} 
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value ="/delete",method = RequestMethod.POST)
	public HashMap<String,String> delete(final Integer id){
		final SysAssetDeposit sysAssetDeposit = this.findById(id);
		if(sysAssetDeposit==null){
			throw new CrowdException("sys.asset.deposit.not_exists");
		}
		this.sysAssetDepositService.delete(id);
		return Message.OK;
	}
}
